package com.axzhengxin.platform.business.controller;

import com.axzhengxin.platform.afterloan.domain.FspCorInf;
import com.axzhengxin.platform.afterloan.service.IFspCorInfoService;
import com.axzhengxin.platform.business.domain.BizApplyWarrantRelative;
import com.axzhengxin.platform.business.domain.BizWarrantInfo;
import com.axzhengxin.platform.business.service.IBizGuaranteeService;
import com.axzhengxin.platform.common.service.IDbExecService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysDept;
import com.ruoyi.common.utils.DateUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.text.ParseException;
import java.util.List;

/**
 * @author zhj
 * 担保业务
 */
@Slf4j
@RestController
@RequestMapping("/guarantee")
public class BizGuaranteeController extends BaseController {

    @Autowired
    private IBizGuaranteeService bizGuaranteeService;

    @Autowired
    private IDbExecService dbExecService;

    @Autowired
    private IFspCorInfoService fspCorInfoService;


    /**
     * 申请担保
     *
     * @return
     */
    @PreAuthorize("@ss.hasPermi('guarantee:applyGuarantee')")
    @PostMapping("/applyGuarantee")
    public AjaxResult applyGuarantee(BizApplyWarrantRelative bizApplyWarrantRelative) {


        return bizGuaranteeService.applyGuarantee(bizApplyWarrantRelative);
    }

    /**
     * 获取担保公司下拉框
     */
    @PreAuthorize("@ss.hasPermi('guarantee:getGuaranteeCom')")
    @PostMapping("/getGuaranteeCom")
    public List<SysDept> getGuaranteeCom() {
        return bizGuaranteeService.getGuaranteeCom();
    }


    /**
     * 担保详情 -非关联需求
     *
     * @return
     */
    @PostMapping("/detail")
    public BizWarrantInfo detail(String warrantId) {
        BizWarrantInfo bizWarrantInfo = bizGuaranteeService.detail(warrantId);
        String customerId = bizWarrantInfo.getCustomerId();
        FspCorInf fspCorInf = fspCorInfoService.selectFspCorInfByCustomerId(customerId);
        if (null != fspCorInf) {
            try {
                String customerName = fspCorInf.getName();
                dbExecService.getJbxxByEntName(customerName)
                        .ifPresent(result -> {
                            fspCorInf.setEconat(result.getString("ECONAT"));
                            fspCorInf.setEntState(result.getString("STATE"));
                            fspCorInf.setOpScope(result.getString("OPSCOPE"));
                            fspCorInf.setEntTypeName(result.getString("ENTTYPENAME"));
                            try {
                                fspCorInf.setEstDate(DateUtils.parseDate(result.getString("ESTDATE"), "yyyy-MM-dd HH:mm:ss"));
                                fspCorInf.setApprDate(DateUtils.parseDate(result.getString("APPRDATE"), "yyyy-MM-dd HH:mm:ss"));
                            } catch (ParseException e) {
                                log.error("解析日期字符串发生异常", e);
                            }
                        });
            } catch (Exception e) {
                log.error("远程接口调用发生异常", e);
                e.printStackTrace();
            }
            bizWarrantInfo.setFspCorInf(fspCorInf);
        }
            return bizWarrantInfo;
    }

}
